﻿Imports System.Text
Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.Windows
Imports Microsoft.VisualStudio.Text.Editor
Imports System.Windows.Media

Namespace VsSpeakerCompanion.UI
	Friend Class SidebarPresenter
		Implements IWidget
        Private WithEvents view As Sidebar

        Private WithEvents context As Context
		Private uiService As IUIService

		Private loginPresenter As LoginControlPresenter
		Private widgetsPresenter As WidgetsControlPresenter

        Private WithEvents endingSessionControl As EndingSessionControl

		Friend Sub New(ByVal manager As WidgetManager, ByVal context As Context, ByVal uiService As IUIService)
            Me.view = New Sidebar

			Me.uiService = uiService
            Me.context = context

			loginPresenter = New LoginControlPresenter(context, manager, uiService)
			widgetsPresenter = New WidgetsControlPresenter(Me.context)

            endingSessionControl = New EndingSessionControl

			Me.view.SetContent(loginPresenter)
		End Sub

        Private Sub OnEndingSessionControlComitted() Handles endingSessionControl.Comitted
            Me.uiService.ExecuteOperation(Sub() Me.context.EndSession())
            RestoreViewExpandStatusBeforeStartPoweringOff()
        End Sub

        Private Sub OnEndingSessionControlCancelled() Handles endingSessionControl.Cancelled
            Me.view.SetContent(Me.widgetsPresenter)
            RestoreViewExpandStatusBeforeStartPoweringOff()
        End Sub

		Private Sub RestoreViewExpandStatusBeforeStartPoweringOff()
            If Not viewWasExpandedBeforeStartPoweringOff Then
                Me.view.Collapse()
            End If
		End Sub

		Private viewWasExpandedBeforeStartPoweringOff As Boolean

        Private Sub OnSidebarViewPowerOff() Handles view.PowerOff
            Me.view.SetContent(Me.endingSessionControl)
            Me.viewWasExpandedBeforeStartPoweringOff = Me.view.IsExpanded
            Me.view.Expand()
            Me.endingSessionControl.Start()
        End Sub

        Private Sub OnContextSessionStarted() Handles context.SessionStarted
            Me.view.UpdatePowerOnOffStatus(True)
            Me.view.footer.Visibility = Visibility.Collapsed
            Me.view.SetContent(Me.widgetsPresenter)
        End Sub

        Private Sub OnContextSessionEnded() Handles context.SessionEnded
            Me.view.UpdatePowerOnOffStatus(False)
            Me.view.footer.Visibility = Visibility.Visible
            Me.view.SetContent(Me.loginPresenter)
        End Sub

		Friend Sub Add(ByVal widget As IWidget)
			Me.widgetsPresenter.Add(widget)
		End Sub

		Friend Sub Dock(ByVal textView As IWpfTextView)
			Me.view.SetHeight(textView.VisualElement.ActualHeight)
			Canvas.SetLeft(Me.view, textView.VisualElement.ActualWidth + textView.ViewportLeft - Me.view.ActualWidth)
		End Sub

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
        Public ReadOnly Property VisualElement As System.Windows.UIElement Implements IWidget.VisualElement
            Get
                Return Me.view
            End Get
        End Property

		''' <summary>
		''' See <see cref="IWidget"/> for more information
		''' </summary>
		Public Sub Reset() Implements IWidget.Reset
			Me.widgetsPresenter.Reset()
		End Sub
	End Class
End Namespace